<template>
  <view>
   <view v-for="(item,i) in goods_list" :key='i' @click="gotoDetail(item)">
     <my-goods :goods="item"></my-goods>
   </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          query: '',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goods_list:[],
        total:0,
        isloading:false
      };
    },
    methods:{
    async getGoodsList(cc){
      this.isloading = true
      const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
      if (res.meta.status !== 200) return uni.$showMsg()
      // 为数据赋值
      this.goods_list = [...this.goods_list,...res.message.goods]
      this.total = res.message.total
       this.isloading = false
       cc&&cc()
    },
    gotoDetail(item){
      console.log(item)
      uni.navigateTo({
        url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
      })
    }
    },
    onLoad(options){
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    // 触发上拉事件
    onReachBottom(){
      if(this.queryObj.pagesize* this.queryObj.pagenum >= this.total) return uni.$showMsg('数据已加载完成')
      if (this.isloading) return
      this.queryObj.pagenum += 1
      this.getGoodsList()
    },
    // 下拉刷新
    onPullDownRefresh(){
      this.queryObj.pagenum = 1
      this.total = 0
      this.isloading = false
      this.goods_list = []
      
      // 重新发起请求
      this.getGoodsList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">
.goods-item{
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;
  .goods-item-left{
    margin-right: 5px;
    .goods-pic{
      width: 100px;
      height: 100px;
      display: block;
    }
  };
  .goods-item-right{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .goods-name{
      font-size: 13px;
    };
    .goods-info{
      .goods-price{
        font-size: 16px;
        color: #c00000;
      }
    }
  }
}



</style>
